Code
library(gt)
With the gt package, anyone can make wonderful-looking tables using the R programming language. The gt philosophy: we can construct a wide variety of useful tables with a cohesive set of table parts. These include the table header, the stub, the column labels and spanner column labels, the table body, and the table footer.
library(gt)
library(gtExtras)
# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"
# Create a gt table based on preprocessed
# `sp500` table data
sp500 |>
dplyr::filter(date >= start_date & date <= end_date) |>
dplyr::select(-adj_close) |>
gt() |>
tab_header(
# title
title = "S&P 500",
# subtitle
subtitle = glue::glue("{start_date} to {end_date}")
) |>
# format currency
fmt_currency() |>
# format date
fmt_date(columns = date, date_style = "wd_m_day_year") |>
# format suffixing
fmt_number(columns = volume, suffixing = TRUE) %>%
# foot note with link
tab_source_note(
gt_hyperlink('Reference', 'https://gt.rstudio.com/')
) %>%
# make images url to images
#gt_img_rows(columns = image, img_source = "web", height = 120) %>%
# align columns
cols_align(
align = c("center"),
columns = everything()
)
S&P 500 | |||||
---|---|---|---|---|---|
2010-06-07 to 2010-06-14 | |||||
date | open | high | low | close | volume |
Mon, Jun 14, 2010 | $1,095.00 | $1,105.91 | $1,089.03 | $1,089.63 | 4.43B |
Fri, Jun 11, 2010 | $1,082.65 | $1,092.25 | $1,077.12 | $1,091.60 | 4.06B |
Thu, Jun 10, 2010 | $1,058.77 | $1,087.85 | $1,058.77 | $1,086.84 | 5.14B |
Wed, Jun 9, 2010 | $1,062.75 | $1,077.74 | $1,052.25 | $1,055.69 | 5.98B |
Tue, Jun 8, 2010 | $1,050.81 | $1,063.15 | $1,042.17 | $1,062.00 | 6.19B |
Mon, Jun 7, 2010 | $1,065.84 | $1,071.36 | $1,049.86 | $1,050.47 | 5.47B |
Reference |
There are several R packages that either use gt to generate tabular outputs or extend gt in amazing ways. Here is a short list of some of these great packages:
https://gt.rstudio.com/
https://www.youtube.com/watch?v=E3ubwU5Uyqw
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
https://rstudio.github.io/distill/tables.html
---
title: "Data table"
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---
# Data table with GT package
{width="200"}
With the gt package, anyone can make wonderful-looking tables using the R programming language. The gt philosophy: we can construct a wide variety of useful tables with a cohesive set of table parts. These include the table header, the stub, the column labels and spanner column labels, the table body, and the table footer.


```{r}
library(gt)
```
```{r}
library(gt)
library(gtExtras)
# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"
# Create a gt table based on preprocessed
# `sp500` table data
sp500 |>
dplyr::filter(date >= start_date & date <= end_date) |>
dplyr::select(-adj_close) |>
gt() |>
tab_header(
# title
title = "S&P 500",
# subtitle
subtitle = glue::glue("{start_date} to {end_date}")
) |>
# format currency
fmt_currency() |>
# format date
fmt_date(columns = date, date_style = "wd_m_day_year") |>
# format suffixing
fmt_number(columns = volume, suffixing = TRUE) %>%
# foot note with link
tab_source_note(
gt_hyperlink('Reference', 'https://gt.rstudio.com/')
) %>%
# make images url to images
#gt_img_rows(columns = image, img_source = "web", height = 120) %>%
# align columns
cols_align(
align = c("center"),
columns = everything()
)
```
## Packages that use or extend gt
There are several R packages that either use gt to generate tabular outputs or extend gt in amazing ways. Here is a short list of some of these great packages:
- **gtsummary** ([GITHUB](https://github.com/ddsjoberg/gtsummary), [WEBSITE](https://www.danieldsjoberg.com/gtsummary/))
- **gtExtras** ([GITHUB](https://github.com/jthomasmock/gtExtras), [WEBSITE](https://jthomasmock.github.io/gtExtras/))
- **pointblank** ([GITHUB](https://github.com/rstudio/pointblank), [WEBSITE](https://rstudio.github.io/pointblank/))
- **tfrmt** ([GITHUB](https://github.com/GSK-Biostatistics/tfrmt), [WEBSITE](https://gsk-biostatistics.github.io/tfrmt/))
- **gto** ([GITHUB](https://github.com/GSK-Biostatistics/gto))
## reference:
https://gt.rstudio.com/
# Data table with reactable
```{r}
library(reactable)
library(reactablefmtr)
mtcars %>% reactable()%>%
# Main title
reactablefmtr::add_title(
title = 'Main title'
) %>%
# sub title
reactablefmtr::add_subtitle(
subtitle = 'sub title',
font_weight = 'normal'
)
```
## reference:
https://www.youtube.com/watch?v=E3ubwU5Uyqw
# Data table with kable
```{r}
library(knitr)
mtcars %>% head() %>% kable()
```
## pages data
```{r}
library(rmarkdown)
mtcars %>% paged_table()
```
## reference:
https://rstudio.github.io/distill/tables.html